package com.wehealth.electrocardiograph_two_client.presenter

import android.widget.Switch
 import com.wehealth.electrocardiograph_two_client.R
import com.wehealth.electrocardiograph_two_client.activity.SettingActivity
 import com.wehealth.electrocardiograph_two_client.databinding.ActivitySettingBinding
import com.wehealth.three.lib_common.route.RouteFlag
import com.wehealth.three.lib_common.route.RouteHelper
import com.wehealth.three.lib_common.utils.SPUtil
import com.wehealth.three.lib_connect.constant.ConstantKey
import com.wehealth.three.lib_connect.constant.ConstantKey.SETTING_SAVE_FILE_TYPE_KEY
import com.wehealth.three.lib_connect.constant.ConstantKey.SETTING_SEARCH_TYPE_KEY

/****
 * 封装的是设置页面中的所有的业务逻辑
 */
class SettingPresenter(private val activity: SettingActivity) {

    fun finishPage() {
        activity.finish()
    }

    /***
     * 路由到关于页面
     */
    fun navigationAboutPage() {
        RouteHelper.startActivity(activity, destination = RouteFlag.about_page)
    }

    /***
     * 设置是否播放指导视频
     */
    fun saveVideoState(switch: Switch) {
        if (switch.isChecked) {
            SPUtil.putBoolean(ConstantKey.SETTING_IS_PLAY_VIDEO_KEY, true)
        } else {
            SPUtil.putBoolean(ConstantKey.SETTING_IS_PLAY_VIDEO_KEY, false)
        }
    }

    /***
     * 设置是否起搏功能开关
     */
    fun savePacingState(switch: Switch) {
        if (switch.isChecked) {
            SPUtil.putBoolean(ConstantKey.SETTING_IS_PACE_KEY, true)
        } else {
            SPUtil.putBoolean(ConstantKey.SETTING_IS_PACE_KEY, false)
        }
    }

    /***
     * 设置当前的测量模式
     */
    fun saveTestTypeState(item: ActivitySettingBinding) {
        when (SPUtil.getInt(SETTING_SAVE_FILE_TYPE_KEY, 0)) {
            0 -> {
                item.setSavefileAuto.isChecked = true
            }

            1 -> {
                item.setSavefileManual.isChecked = true
            }

            2 -> {
                item.setSavefile24h.isChecked = true
            }
        }
        item.setSaveFileRg.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.set_savefile_24h -> {
                    SPUtil.putInt(SETTING_SAVE_FILE_TYPE_KEY, 2)
                }

                R.id.set_savefile_auto -> {
                    SPUtil.putInt(SETTING_SAVE_FILE_TYPE_KEY, 0)
                }

                R.id.set_savefile_manual -> {
                    SPUtil.putInt(SETTING_SAVE_FILE_TYPE_KEY, 1)
                }
            }
        }
    }

    /***
     * 设置当前的测量模式
     */
    fun saveSearchTypeState(item: ActivitySettingBinding) {
        when (SPUtil.getInt(SETTING_SEARCH_TYPE_KEY, 0)) {
            0 -> {
                item.setSearchAll.isChecked = true
            }

            1 -> {
                item.setSearchClassic.isChecked = true
            }

            2 -> {
                item.setSearchBle.isChecked = true
            }
        }
        item.settingSearchTypeContainerGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.set_search_all -> {
                    SPUtil.putInt(SETTING_SEARCH_TYPE_KEY, 0)
                }

                R.id.set_search_classic -> {
                    SPUtil.putInt(SETTING_SEARCH_TYPE_KEY, 1)
                }

                R.id.set_search_ble -> {
                    SPUtil.putInt(SETTING_SEARCH_TYPE_KEY, 2)
                }
            }
        }
    }
}